﻿using Biz.EF.Gateway.Monitor;
using Biz.EF.Platform.SG;
using Biz.Process;
using Hjt212Parser;
using log4net;
using System;
using System.ServiceProcess;
using System.Threading;

namespace DataProcessor_jiangsuTongzhouSG
{
    public partial class BizProcessor : ServiceBase
    {
        private ILog log = LogManager.GetLogger(typeof(BizProcessor));
        int milliSeconds = 10;

        public BizProcessor()
        {
            InitializeComponent();
        }
        //test
        public void Main()
        {
            OnStart(null);
        }
        protected override void OnStart(string[] args)
        {
            //启动各路线程
            new Thread(ProcessData).Start();

            new Thread(ProcessDownCmds).Start();
            log.Info("各路线程启动完成...");
        }
        protected override void OnStop()
        {
            log.Info("The processor Stoped!");
        }
        private void ProcessData(object state)
        {
            while (true)
            {
                try
                {
                    CommonProcessor.Process();
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }
        private void ProcessDownCmds(object state)
        {
            while (true)
            {
                try
                {
                    using (var gCtx = new GatewayMonitorContext())
                    using (var pCtx = new PlatformXpfContext())
                    {
                        ProcessorDownline.GenDownlineCmdToGateway(gCtx, pCtx);
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(milliSeconds);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }

    }
}
